package cn.xyinping.controller;

import cn.xyinping.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 27266
 * @desrciption 订单接口
 * @date 2021/11/14
 */
@RestController
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;  // 远程调用用户服务
    // 标记降级方法
    @HystrixCommand(fallbackMethod = "getByIdFillback")
    @RequestMapping("/getById/{id}")
    public User getById(@PathVariable("id") Long id){
        String url = "http://SERVER-USER/getById/"+id;
       // int i= 1/0;
        User user = restTemplate.getForObject(url, User.class);
        System.out.println(user.getUsername());
        return user;
    }
    // 降级方法
    public User getByIdFillback(@PathVariable("id")Long id){
        // 兜底方法
        return new User(-1L,"降级方法","系统异常");
    }
}
